package com.mytools.common.pattern.itreator02;

import lombok.Data;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author LJK
 * @description
 * @date 2025/2/10
 */
@Data
@ToString
public class User implements Iterable {
    String name;
    Integer age;
    Boolean sex;

    @NotNull
    @Override
    public Iterator iterator() {
        return new Itr();
    }

    class Itr implements Iterator<String> {
        private final int max = 3;
        private int cursor = 0;

        @Override
        public boolean hasNext() {
            return cursor < max;
        }

        @Override
        public String next() {
            return switch (cursor++) {
                case 0 -> name != null ? name : "null";
                case 1 -> age != null ? age.toString() : "null";
                case 2 -> sex != null ? sex.toString() : "null";
                default -> throw new NoSuchElementException();
            };
        }
    }
}
